﻿package {
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	import idv.cjcat.display.*;
	import idv.cjcat.display.dynashape.*;
	import idv.cjcat.geom.*;
	
	public class Main extends Sprite {
		
		public function Main():void {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private var pointCount:uint = 400;
		private var ds:DynaShape;
		private var lineStyle:LineStyle;
		private var fill:Fill;
		private var dg:DynaGraphics;
		private var vectors:Vector.<Vector2>;
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			vectors = new Vector.<Vector2>();
			var i:uint;
			for (i = 0; i < pointCount; i++) {
				var vec:Vector2 = new Vector2(800 / (pointCount - 1) * (i), 300);
				vectors.push(vec);
			}
			vectors.push(vectors[0]);
			
			ds = new DynaShape();
			dg = new DynaGraphics();
			dg.winding = "evenOdd";
			lineStyle = new LineStyle(0, 0x808080);
			fill = new Fill(0xCCCCCC);
			dg = new DynaGraphics();
			dg.lineStyle(lineStyle);
			dg.beginFill(fill);
			dg.moveTo(vectors[0]);
			for (i = 1; i < vectors.length; i++) dg.lineTo(vectors[i]);
			dg.endFill();
			
			ds.add(dg);
			addChild(ds);
			
			addEventListener(Event.ENTER_FRAME, loop);
		}
		
		private function loop(e:Event):void {
			for (var i:uint = 0; i < vectors.length; i++) vectors[i].y = 300 + 100 * Math.sin(getTimer() / 400 + 0.09 * i);
			fill.alpha = 0.5 + 0.5 * Math.sin(getTimer() / 333);
			ds.render();
		}
	}
}